/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.policy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.modules.policy.entity.LabelItem;
import org.springblade.modules.policy.mapper.LabelItemMapper;
import org.springblade.modules.policy.service.ILabelItemService;
import org.springblade.modules.policy.vo.LabelItemVO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springblade.common.cache.CacheNames.*;

/**
 * 字典表 服务实现类
 *
 * @author Blade
 * @since 2022-07-05
 */
@Service
public class LabelItemServiceImpl extends BaseServiceImpl<LabelItemMapper, LabelItem> implements ILabelItemService {

	@Override
	public IPage<LabelItemVO> selectLabelItemPage(IPage<LabelItemVO> page, LabelItemVO labelItem) {
		return page.setRecords(baseMapper.selectLabelItemPage(page, labelItem));
	}

	@Override
	@Cacheable(cacheNames = LABEL_TREE, key = "#type")
	public List<LabelItemVO> selectTree(String type) {
		return ForestNodeMerger.merge(baseMapper.getTree(type));
	}

	@Override
	public List<LabelItemVO> tree() {
		return ForestNodeMerger.merge(baseMapper.tree());
	}

	@Override
	@Cacheable(cacheNames = LABEL_VALUE, key = "#type+'_'+#labelValue")
	public String getLabel(String type, Integer labelValue) {
		return Func.toStr(baseMapper.getLabel(type, labelValue), StringPool.EMPTY);
	}

	@Cacheable(cacheNames = LABEL_LIST, key = "#type")
	@Override
	public List<LabelItem> getList(String type) {
		return baseMapper.getList(type);
	}

	@Override
	@CacheEvict(cacheNames = {LABEL_LIST, LABEL_VALUE}, allEntries = true)
	public boolean submit(LabelItem label) {
		LambdaQueryWrapper<LabelItem> lqw = Wrappers.<LabelItem>query().lambda().eq(LabelItem::getType, label.getType()).eq(LabelItem::getLabel, label.getLabel());
		Long cnt = baseMapper.selectCount((Func.isEmpty(label.getId())) ? lqw : lqw.notIn(LabelItem::getId, label.getId()));
		if (cnt > 0) {
			throw new ServiceException("当前标签键值已存在!");
		}
		return saveOrUpdate(label);
	}

}
